<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" :label-width="100">
			<el-form-item label="小游戏id" prop="appid">
				<el-input v-model="dataForm.appid" placeholder="小游戏id"></el-input>
			</el-form-item>
			<el-form-item label="小游戏名字" prop="appname">
				<el-input v-model="dataForm.appname" placeholder="小游戏名字"></el-input>
			</el-form-item>
			<el-form-item label="游戏简介" prop="briefIntro">
				<el-input v-model="dataForm.briefIntro" placeholder="游戏简介"></el-input>
			</el-form-item>
			<el-form-item label="游戏logo" prop="logo">
				<el-input v-model="dataForm.logo" placeholder="游戏logo"></el-input>
			</el-form-item>
			<el-form-item label="二级分类id" prop="category2Id">
				<el-input v-model="dataForm.category2Id" placeholder="二级分类id"></el-input>
			</el-form-item>
			<el-form-item label="二级分类名字" prop="category2Name">
				<el-input v-model="dataForm.category2Name" placeholder="二级分类名字"></el-input>
			</el-form-item>
			<el-form-item label="三级分类id" prop="category3Id">
				<el-input v-model="dataForm.category3Id" placeholder="三级分类id"></el-input>
			</el-form-item>
			<el-form-item label="三级分名字" prop="category3Name">
				<el-input v-model="dataForm.category3Name" placeholder="三级分名字"></el-input>
			</el-form-item>
			<el-form-item label="小游戏原始id" prop="userName">
				<el-input v-model="dataForm.userName" placeholder="小游戏原始id"></el-input>
			</el-form-item>
			<el-form-item label="游戏封面" prop="banner">
				<el-input v-model="dataForm.banner" placeholder="游戏封面"></el-input>
			</el-form-item>
			<el-form-item label="宣传视频对象" prop="videoInfo">
				<el-input v-model="dataForm.videoInfo" placeholder="宣传视频对象"></el-input>
			</el-form-item>
			<el-form-item label="图片列表" prop="publicityImg">
				<el-input v-model="dataForm.publicityImg" placeholder="图片列表"></el-input>
			</el-form-item>
			<el-form-item label="方形icon" prop="logoSquare">
				<el-input v-model="dataForm.logoSquare" placeholder="方形icon"></el-input>
			</el-form-item>
			<el-form-item label="cps分成比例" prop="cpsPercent">
				<el-input v-model="dataForm.cpsPercent" placeholder="cps分成比例"></el-input>
			</el-form-item>
			<el-form-item label="是否pc不可用" prop="isPcUnavailable">
				<el-input v-model="dataForm.isPcUnavailable" placeholder="是否pc不可用"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useGameApi, useGameSubmitApi } from '@/api/kyy/game'

const emit = defineEmits(['refreshDataList'])

const visible = defineModel<boolean>('visible')
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	appid: '',
	appname: '',
	briefIntro: '',
	logo: '',
	category2Id: '',
	category2Name: '',
	category3Id: '',
	category3Name: '',
	userName: '',
	banner: '',
	videoInfo: '',
	publicityImg: '',
	logoSquare: '',
	cpsPercent: '',
	isPcUnavailable: ''
})

const init = (id?: number) => {
	if (id) {
		getGame(id)
	}
}

const getGame = (id: number) => {
	useGameApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	appid: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	appname: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	briefIntro: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	logo: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	category2Id: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	category2Name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	category3Id: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	category3Name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	userName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	banner: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	videoInfo: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	publicityImg: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	logoSquare: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	cpsPercent: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	isPcUnavailable: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) {
			return false
		}

		useGameSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
